using System;
using System.Collections.Generic;
using System.Text;

namespace Tetris
{
    class Block
    {
        public static Random r = new Random();
        public bool[,] tiles = new bool[4, 4];
        
        public void RotateRight()
        {
            bool[,] t = new bool[4, 4];

            for (int y = 0; y < 4; y++)
                for (int x = 0; x < 4; x++)
                    t[x, y] = tiles[3 - y, x];

            tiles = t;
        }

        public void RotateLeft()
        {
            bool[,] t = new bool[4, 4];

            for (int y = 0; y < 4; y++)
                for (int x = 0; x < 4; x++)
                    t[3 - y, x] = tiles[x, y];

            tiles = t;
        }

        public void Rotate(Direction direction)
        {
            switch (direction)
            {
                case Direction.S:
                    RotateRight();
                    RotateRight();
                    return;
                case Direction.E:
                    RotateRight();
                    return;
                case Direction.W:
                    RotateLeft();
                    return;
            }
        }

        public static Block CreateI
        {
            get
            {
                Block b = new Block();
                b.tiles[1, 0] = true;
                b.tiles[1, 1] = true;
                b.tiles[1, 2] = true;
                b.tiles[1, 3] = true;
                return b;
            }
        }

        public static Block CreateT
        {
            get
            {
                Block b = new Block();
                b.tiles[0, 1] = true;
                b.tiles[1, 1] = true;
                b.tiles[2, 1] = true;
                b.tiles[1, 2] = true;               
                return b;
            }
        }

        public static Block CreateL
        {
            get
            {
                Block b = new Block();
                b.tiles[1, 3] = true;
                b.tiles[1, 2] = true;
                b.tiles[1, 1] = true;
                b.tiles[2, 1] = true;
                return b;
            }
        }

        public static Block CreateJ
        {
            get
            {
                Block b = new Block();
                b.tiles[2, 3] = true;
                b.tiles[2, 2] = true;
                b.tiles[2, 1] = true;
                b.tiles[1, 1] = true;
                return b;
            }
        }

        public static Block CreateZ
        {
            get
            {
                Block b = new Block();
                b.tiles[0, 2] = true;
                b.tiles[1, 2] = true;
                b.tiles[1, 1] = true;
                b.tiles[2, 1] = true;
                return b;
            }
        }

        public static Block CreateS
        {
            get
            {
                Block b = new Block();
                b.tiles[2, 2] = true;
                b.tiles[1, 2] = true;
                b.tiles[1, 1] = true;
                b.tiles[0, 1] = true;
                return b;
            }
        }

        public static Block CreateO
        {
            get
            {
                Block b = new Block();
                b.tiles[1, 1] = true;
                b.tiles[1, 2] = true;
                b.tiles[2, 1] = true;
                b.tiles[2, 2] = true;
                return b;
            }
        }

        public static Block CreateRandom
        {
            get
            {                
                switch (r.Next(7))
                {
                    case 0:
                        return CreateI;
                    case 1:
                        return CreateJ;
                    case 2:
                        return CreateL;
                    case 3:
                        return CreateO;
                    case 4:
                        return CreateT;
                    case 5:
                        return CreateZ;
                    case 6:
                        return CreateS;
                }
                return null;
            }
        }

        public Block Clone()
        {
            Block block = new Block();
            for (int x = 0; x < 4; x++)
                for (int y = 0; y < 4; y++)
                    block.tiles[x, y] = this.tiles[x, y];
            return block;
        }
    }
}
